import * as zrender from 'zrender';
import { getUniquePersons } from '../../utils/helper';
import { PersonRow } from './person-row';

/**
 * 表格主体组件
 * 负责渲染人员行和排班数据
 */
export class TableBody {
  private zr: any;
  private schedules: PersonSchedule[];
  private chartState: ChartState;
  private bodyGroup: zrender.Group;
  private personRows: PersonRow[] = [];
  private rowReorderCallback: RowReorderCallback | null = null;
  private currentGroup: any = null;

  constructor(zr: any, schedules: PersonSchedule[], chartState: ChartState) {
    this.zr = zr;
    this.schedules = schedules;
    this.chartState = chartState;
    this.bodyGroup = new zrender.Group();
  }

  /**
   * 渲染表格主体
   */
  render() {
    this.bodyGroup.removeAll();
    this.personRows = [];

    const { responsiveConfig } = this.chartState;
    const { timeScaleHeight, barHeight } = responsiveConfig;
    const uniquePersons = getUniquePersons(this.schedules);

    // 绘制网格线
    this.drawGridLines();

    // 渲染每一行
    uniquePersons.forEach((schedule, index) => {
      const y = timeScaleHeight + index * (barHeight + 1);

      const personRow = new PersonRow(
        this.zr,
        schedule,
        this.chartState,
        y,
        index,
      );

      personRow.render();
      this.personRows.push(personRow);
    });

    // 添加到画布
    this.zr.add(this.bodyGroup);
  }

  /**
   * 绘制网格线
   */
  private drawGridLines() {
    const { responsiveConfig } = this.chartState;
    const { personColumnWidth, unitWidth, timeScaleHeight, barHeight } = responsiveConfig;
    const { canvasWidth, canvasHeight, timeScaleUnits } = this.chartState;
    const uniquePersons = getUniquePersons(this.schedules);

    // 绘制垂直网格线
    for (let i = 0; i <= timeScaleUnits; i++) {
      const x = personColumnWidth + i * unitWidth;

      const verticalLine = new zrender.Line({
        shape: {
          x1: x,
          y1: timeScaleHeight,
          x2: x,
          y2: canvasHeight,
        },
        style: {
          stroke: '#e9ecef',
          lineWidth: 1,
        },
      });

      this.bodyGroup.add(verticalLine);
    }

    // 绘制水平网格线
    for (let i = 0; i <= uniquePersons.length; i++) {
      const y = timeScaleHeight + i * (barHeight + 1);

      const horizontalLine = new zrender.Line({
        shape: {
          x1: 0,
          y1: y,
          x2: canvasWidth,
          y2: y,
        },
        style: {
          stroke: '#e9ecef',
          lineWidth: 1,
        },
      });

      this.bodyGroup.add(horizontalLine);
    }
  }

  /**
   * 更新数据和状态
   */
  update(schedules: PersonSchedule[], chartState: ChartState) {
    this.schedules = schedules;
    this.chartState = chartState;
  }

  /**
   * 设置行重排序回调
   */
  setRowReorderCallback(callback: RowReorderCallback) {
    this.rowReorderCallback = callback;
    console.log('setRowReorderCallback', this.rowReorderCallback);
  }

  /**
   * 设置当前操作组
   */
  setCurrentGroup(group: any) {
    this.currentGroup = group;
    console.log('setCurrentGroup', this.currentGroup);
  }
}
